SET(CLASSPATH "${JBLAS}:${SHOGUN_JAR}:${CMAKE_CURRENT_BINARY_DIR}")
SET(JAVAOPTS "-Xmx1024m")
SET(JAVA_LIB_PATH "${INTERFACE_JAVA_BUILD_DIR}")

# add test case for each generated example
# (not generated yet so have to fake filenames from META_EXAMPLES list)
FOREACH(META_EXAMPLE ${META_EXAMPLES})
    # assume a structure <target_language>/<category>/listing.sg
    GET_META_EXAMPLE_VARS(${META_EXAMPLE} EXAMPLE_NAME EXAMPLE_REL_DIR EXAMPLE_NAME_WITH_DIR)

	set(COMPILED_JAVA_EXAMPLE "${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}/${EXAMPLE_NAME}.class")
	set(JAVA_META_EXAMPLE_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}/${EXAMPLE_NAME}.java")

	ADD_CUSTOM_COMMAND(OUTPUT ${COMPILED_JAVA_EXAMPLE}
		COMMAND ${Java_JAVAC_EXECUTABLE} -cp ${CLASSPATH}
			-d ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}
			${JAVA_META_EXAMPLE_SOURCE}
		DEPENDS interface_java meta_examples shogun ${JAVA_META_EXAMPLE_SOURCE})

	ADD_CUSTOM_TARGET(java_${EXAMPLE_NAME} ALL
		DEPENDS ${COMPILED_JAVA_EXAMPLE}
	)

	LIST(APPEND GENERATED_JAVA_EXAMPLES java_${EXAMPLE_NAME})

	add_test(NAME generated_java-${EXAMPLE_NAME_WITH_DIR}
			WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}
			COMMAND ${Java_JAVA_EXECUTABLE} ${JAVAOPTS}
				-cp ${CLASSPATH}:${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}
				-Djava.library.path=${JAVA_LIB_PATH} ${EXAMPLE_NAME})
ENDFOREACH()

add_custom_target(build_java_meta_examples ALL
				 DEPENDS ${GENERATED_JAVA_EXAMPLES}
				 COMMENT "Compiled generated java examples")
